﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using System.Windows.Forms;
using SMS.cntr;
using SMS.dao;
using SMS.util;

namespace SMS.view
{
    public partial class CreateSemesterForm : Form
    {
        private int semesterId;

        public CreateSemesterForm()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            SemesterCntr.Instance.insert(txtSemester.Text);
            bindDataToGridViewSemester();
            txtSemester.Text = "";
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            SemesterCntr.Instance.update(txtSemester.Text, semesterId);           
            bindDataToGridViewSemester();
            txtSemester.Text = txtSemester.Text;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            SemesterCntr.Instance.delete(semesterId);
            bindDataToGridViewSemester();
            txtSemester.Text = "";
        }

        private void CreateSemesterForm_Load(object sender, EventArgs e)
        {
            bindDataToGridViewSemester();
            changeLang(I18nUtil.vnLang);
        }

        private void bindDataToGridViewSemester()
        {
            gridViewSemesters.Rows.Clear();
            DataTable table = SemesterCntr.Instance.findAll();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                gridViewSemesters.Rows.Add(i + 1,
                                           table.Rows[i]["sem_name"].ToString(),
                                           table.Rows[i]["sem_id"].ToString());
                gridViewSemesters.Rows[gridViewSemesters.RowCount - 1].Tag = table.Rows[i];
            }
            gridViewSemesters.ClearSelection();
        }
        
        private void gridViewSemesters_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < gridViewSemesters.SelectedRows.Count; i++)
            {
                String id = gridViewSemesters.SelectedRows[i].Cells[2].Value.ToString();
                String semester = SemesterDao.Instance.findById(Int32.Parse(id))[1].ToString();
                semesterId = Int32.Parse(SemesterDao.Instance.findById(Int32.Parse(id))[0].ToString());
                txtSemester.Text = semester;
            }
        }

        public void changeLang(String lang)
        {
            I18nUtil.init(lang, new ResourceManager(I18nUtil.resourcePath, typeof(CreateSemesterForm).Assembly));

            this.Text = I18nUtil.getString("createSemester");
            label1.Text = I18nUtil.getString("lblSemester");
            btnAdd.Text = I18nUtil.getString("btnAdd");
            btnUpdate.Text = I18nUtil.getString("btnUpdate");
            btnDelete.Text = I18nUtil.getString("btnDelete");
            groupBox1.Text = I18nUtil.getString("gbSemesterList");
            gridViewSemesters.Columns[0].HeaderText = I18nUtil.getString("no");
            gridViewSemesters.Columns[1].HeaderText = I18nUtil.getString("semesterName");
        }
    }
}
